package com.cs.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cs.domain.entity.User;
import com.cs.domain.entity.UserInfo;
import com.cs.user.dao.IUserDao;
import com.cs.user.exception.RegisterException;
import com.cs.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private IUserDao dao;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;



    @Override
    public boolean register(String username, String password, String email) {
        Boolean isRegister = stringRedisTemplate.opsForSet().isMember("username", username);

        if (isRegister == false){
            User user = new User();
            user.setUsername(username);
            user.setPassword(password);
            user.setEmail(email);
            user.setRole("3");
            user.setCreatTime(new Date());

            dao.insert(user);
            stringRedisTemplate.opsForSet().add("username", username);
        }
        return isRegister;
    }

    @Override
    public List<User> login(String username, String password) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_username",username)
                .eq("user_password",password);
        List<User> users = dao.selectList(wrapper);
        return users;
    }

    @Override
    public void deleteUserById(long id) {
        User user = new User();
        user.setId(id);
        dao.deleteById(user);
    }

    @Override
    public Integer updateUser(User user) {
        return dao.updateById(user);
    }
}
